//
//  TokenTableViewController.m
//  iD20
//
//  Created by Gary Curtis on 5/18/10.
//  Copyright 2010 STScI. All rights reserved.
//

#import "TokenTableViewController.h"
#import "iD20AppDelegate.h"

@implementation TokenTableViewController

@synthesize managedObjectContext;
@synthesize tokenArray;
@synthesize addButton;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 // Custom initialization
 }
 return self;
 }
 */

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	NSLog(@"TTVC did load");
	[super viewDidLoad];
	
	managedObjectContext = [FirstGridAppAppDelegate getManagedObjectContext];
	tokenArray = [[NSMutableArray alloc] initWithObjects:nil];
	
	addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
															  target:self action:@selector(addToken)];
	addButton.enabled = YES;
	self.navigationItem.rightBarButtonItem = addButton;
}

- (void)addToken {
	NSLog(@"Add Token");
	Token *token = (Token *)[NSEntityDescription insertNewObjectForEntityForName:@"Token" inManagedObjectContext:managedObjectContext];
	token.name = @"Token";
	NSError *error;
	if (![managedObjectContext save:&error]) {
		// Handle the error.
	}
	
	[tokenArray insertObject:token atIndex:0];
	NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
	[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
						  withRowAnimation:UITableViewRowAnimationFade];
	[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
	
}

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark Table View Delegate Methods

- (void)tableView:(UITableView *)tableView
		didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	//	NSInteger row = [indexPath row];
	NSString *rowValue = @"A Token";
	
	NSString *message = [[NSString alloc] initWithFormat:@"You selected %@", rowValue];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"RowSelected"
													message:message
												   delegate:nil
										  cancelButtonTitle:@"OK"
										  otherButtonTitles:nil];
	[alert show];
	[alert release];
	[message release];
}

#pragma mark -
#pragma mark Table View Data Source Methods

- (NSInteger)tableView:(UITableView *)tableView
		numberOfRowsInSection:(NSInteger)section {
	NSLog(@"Tokens: %d", [tokenArray count]);
	return [tokenArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
		 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
									   reuseIdentifier:SimpleTableIdentifier] autorelease];
	}
	
	NSInteger row = [indexPath row];
	Token *token = [tokenArray objectAtIndex:row];
	cell.textLabel.text = token.name;
	//	cell.textLabel.font = [UIFont boldSystemFontOfSize:80];
	UIImage *image = [UIImage imageNamed:@"star.png"];
	cell.imageView.image = image;
	return cell;
}

@end
